﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IsmsExplorer.Infrastructure;
using IsmsExplorer.Model.AssetRegister;
using IsmsExplorer.Infrastructure.RepositoryFramework;
using IsmsExplorer.Model.Projects;

namespace IsmsExplorer.Application.AssetRegister {
    public static class AssetRegisterService {
        private static IUnitOfWork _unitOfWork;
        private static IAssetRepository _assetRepository;
        private static IAssetCategoryRepository _assetCategoryRepository;
        private static ILocationRepository _locationRepository;
        private static IRoleRepository _roleRepository;

        static AssetRegisterService() {
            _unitOfWork = new UnitOfWork();
            _assetRepository = RepositoryFactory.GetRepository<IAssetRepository, Asset>(_unitOfWork);
            _assetCategoryRepository = RepositoryFactory.GetRepository<IAssetCategoryRepository, AssetCategory>(_unitOfWork);
            _locationRepository = RepositoryFactory.GetRepository<ILocationRepository, Location>(_unitOfWork);
            _roleRepository = RepositoryFactory.GetRepository<IRoleRepository, Role>(_unitOfWork);
        }

        // Location
        public static Location GetAssetLocationByName(string name) {
            return _locationRepository.FindBy(name);
        }

        public static void SaveAssetLocation(Location location) {
            _locationRepository[location.ID] = location;
            _unitOfWork.Commit();
        }

        public static IList<Location> GetAllAssetLocations() {
            return _locationRepository.FindAll();
        }

        public static void DeleteAssetLocation(Location location) {
            _locationRepository.Remove(location);
            _unitOfWork.Commit();
        }

        // Asset Category
        public static AssetCategory GetAssetCategoryByName(string name) {
            return _assetCategoryRepository.FindBy(name);
        }

        public static void SaveAssetCategory(AssetCategory assetCategory) {
            _assetCategoryRepository[assetCategory.ID] = assetCategory;
            _unitOfWork.Commit();
        }

        public static IList<AssetCategory> GetAllAssetCategories() {
            return _assetCategoryRepository.FindAll();
        }

        public static void DeleteAssetCategory(AssetCategory assetCategory) {
            _assetCategoryRepository.Remove(assetCategory);
            _unitOfWork.Commit();
        }

        // Role
        public static Role GetAssetOwnerRoleByName(string name) {
            return _roleRepository.FindBy(name);
        }

        public static void SaveAssetOwnerRole(Role role) {
            _roleRepository[role.ID] = role;
            _unitOfWork.Commit();
        }

        public static IList<Role> GetAllAssetOwnerRoles() {
            return _roleRepository.FindAll();
        }

        public static void DeleteAssetOwnerRole(Role role) {
            _roleRepository.Remove(role);
            _unitOfWork.Commit();
        }

        // Asset
        public static void SaveAsset(Asset asset) {
            _assetRepository[asset.ID] = asset;
            _unitOfWork.Commit();
        }

        public static IList<Asset> GetAllAssetsForProject(Project project) {
            return _assetRepository.FindBy(project);
        }

        public static void DeleteAsset(Asset asset) {
            _assetRepository.Remove(asset);
            _unitOfWork.Commit();
        }

    } // end public static class AssetRegisterService
}
